package com.org.web.api.unishop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.org.platform.model.ResultModel;
import com.org.platform.utils.CommonUtil;
import com.org.web.api.unishop.service.UserService;
import com.org.web.handle.wechat.WechatCommon;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author duheng
 * @Date 2021/10/17 15:26
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

	public static final Map<String, String> userAuthMap = new HashMap<>();

	static {
		userAuthMap.put("o5RjW5Qlgca36tmAlWsK4SACZALY", "15308650573");
	}

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private RestTemplate restTemplate;

	@Value("${wechat.appId}")
	private String appId;

	@Value("${wechat.secret}")
	private String secret;

	@Override
	public ResultModel login(Map<String, Object> params) {
		String mobile = CommonUtil.getMapString(params, "mobile");
		String password = CommonUtil.getMapString(params, "password");
		String loginType = CommonUtil.getMapString(params, "loginType");
		if (CommonUtil.equals(loginType, "wechat")) {
			return wechatLogin(params);
		}
		if (CommonUtil.equals(mobile, "15308650573") && CommonUtil.equals(password, "123456")) {
			String uuid = UUID.randomUUID().toString();
			stringRedisTemplate.opsForValue().set(mobile, uuid, 5, TimeUnit.MINUTES);
			Map<String, Object> result = new HashMap<>();
			result.put("exprieTime", 5 * 60);
			result.put("mobile", mobile);
			result.put("token", uuid);
			return ResultModel.newSuccess(result);
		}
		return ResultModel.newFail("登录失败");
	}

	public ResultModel wechatLogin(Map<String, Object> params) {
		if (null == params || params.isEmpty() || !params.containsKey("wxres")) {
			return ResultModel.newFail("参数错误");
		}
		log.info("wechatLogin params:" + JSONObject.toJSONString(params));
		String wxresString = CommonUtil.getMapString(params, "wxres");
		if (CommonUtil.isNullOrEmpty(wxresString)) {
			return ResultModel.newFail("参数错误");
		}
		Map<String, Object> wxres = JSONObject.parseObject(wxresString);
		if (null == wxres || wxres.isEmpty() || !wxres.containsKey("code")) {
			return ResultModel.newFail("参数错误");
		}
		String code = CommonUtil.getMapString(wxres, "code");
		if (CommonUtil.isNullOrEmpty(code)) {
			return ResultModel.newFail("参数错误");
		}
		String url = WechatCommon.jscode2session + "?appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
		log.info("wechatLogin url:" + url);
		String wechatAuthResult = restTemplate.getForObject(url, String.class);
		if (CommonUtil.isNullOrEmpty(wechatAuthResult)) {
			return ResultModel.newFail("微信授权失败");
		}
		log.info("wechatLogin wechatAuthResult:" + wechatAuthResult);
		Map<String, Object> wechatAuthMap = JSONObject.parseObject(wechatAuthResult);
		if (null == wechatAuthMap || wechatAuthMap.isEmpty() || !wechatAuthMap.containsKey("openid")) {
			return ResultModel.newFail("微信获取用户openid失败");
		}
		String openid = CommonUtil.getMapString(wechatAuthMap, "openid");
		String mobile = userAuthMap.get(openid);
		String uuid = UUID.randomUUID().toString();
		stringRedisTemplate.opsForValue().set(mobile, uuid, 5, TimeUnit.MINUTES);
		Map<String, Object> result = new HashMap<>();
		result.put("exprieTime", 5 * 60);
		result.put("mobile", mobile);
		result.put("token", uuid);
		return ResultModel.newSuccess(result);
	}
}
